package com.music.note_android.ui.editor

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.music.note_android.R
import kotlinx.coroutines.launch

/**
 * 画布测试活动
 * 用于测试画布编辑器的各项功能和性能
 */
class CanvasTestActivity : AppCompatActivity() {
    
    private lateinit var canvasView: DrawingCanvasView
    private lateinit var canvasTester: CanvasTester
    private var isTestRunning = false
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_canvas_editor)
        
        setupViews()
        setupTester()
        setupToolbar()
    }
    
    private fun setupViews() {
        canvasView = findViewById(R.id.drawing_canvas)
        
        // 设置画布为测试模式
        canvasView.setCurrentTool(DrawingTool.PEN)
        canvasView.setCurrentColor(android.graphics.Color.BLACK)
        canvasView.setStrokeWidth(5f)
    }
    
    private fun setupTester() {
        canvasTester = CanvasTester()
    }
    
    private fun setupToolbar() {
        supportActionBar?.apply {
            title = "画布测试"
            setDisplayHomeAsUpEnabled(true)
        }
    }
    
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_canvas_test, menu)
        return true
    }
    
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            android.R.id.home -> {
                finish()
                true
            }
            R.id.action_test_basic -> {
                runBasicTest()
                true
            }
            R.id.action_test_performance -> {
                runPerformanceTest()
                true
            }
            R.id.action_test_memory -> {
                runMemoryTest()
                true
            }
            R.id.action_stop_test -> {
                stopCurrentTest()
                true
            }
            R.id.action_clear_canvas -> {
                canvasView.clearCanvas()
                true
            }
            R.id.action_toggle_grid -> {
                canvasView.setGridVisible(!canvasView.isGridVisible())
                true
            }
            R.id.action_toggle_ruler -> {
                canvasView.setRulerVisible(!canvasView.isRulerVisible())
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
    
    /**
     * 运行基础功能测试
     */
    private fun runBasicTest() {
        if (isTestRunning) {
            Toast.makeText(this, "测试正在进行中，请稍候", Toast.LENGTH_SHORT).show()
            return
        }
        
        lifecycleScope.launch {
            isTestRunning = true
            
            canvasTester.runBasicFunctionalityTest(
                canvasView,
                object : CanvasTester.TestCallback {
                    override fun onTestStarted(testName: String) {
                        runOnUiThread {
                            Toast.makeText(this@CanvasTestActivity, "开始$testName", Toast.LENGTH_SHORT).show()
                        }
                    }
                    
                    override fun onTestProgress(progress: Int, message: String) {
                        runOnUiThread {
                            supportActionBar?.subtitle = "$message ($progress%)"
                        }
                    }
                    
                    override fun onTestCompleted(testName: String, result: CanvasTester.TestResult) {
                        runOnUiThread {
                            isTestRunning = false
                            supportActionBar?.subtitle = null
                            
                            val status = if (result.passed) "通过" else "失败"
                            val details = result.details.joinToString("\n")
                            
                            Toast.makeText(
                                this@CanvasTestActivity,
                                "$testName $status\n$details",
                                Toast.LENGTH_LONG
                            ).show()
                        }
                    }
                    
                    override fun onTestFailed(testName: String, error: String) {
                        runOnUiThread {
                            isTestRunning = false
                            supportActionBar?.subtitle = null
                            
                            Toast.makeText(
                                this@CanvasTestActivity,
                                "$testName 失败: $error",
                                Toast.LENGTH_LONG
                            ).show()
                        }
                    }
                }
            )
        }
    }
    
    /**
     * 运行性能压力测试
     */
    private fun runPerformanceTest() {
        if (isTestRunning) {
            Toast.makeText(this, "测试正在进行中，请稍候", Toast.LENGTH_SHORT).show()
            return
        }
        
        lifecycleScope.launch {
            isTestRunning = true
            
            canvasTester.runPerformanceStressTest(
                canvasView,
                object : CanvasTester.TestCallback {
                    override fun onTestStarted(testName: String) {
                        runOnUiThread {
                            Toast.makeText(this@CanvasTestActivity, "开始$testName", Toast.LENGTH_SHORT).show()
                        }
                    }
                    
                    override fun onTestProgress(progress: Int, message: String) {
                        runOnUiThread {
                            supportActionBar?.subtitle = "$message ($progress%)"
                        }
                    }
                    
                    override fun onTestCompleted(testName: String, result: CanvasTester.TestResult) {
                        runOnUiThread {
                            isTestRunning = false
                            supportActionBar?.subtitle = null
                            
                            val status = if (result.passed) "通过" else "失败"
                            val details = result.details.joinToString("\n")
                            
                            Toast.makeText(
                                this@CanvasTestActivity,
                                "$testName $status\n耗时: ${result.duration}ms\n$details",
                                Toast.LENGTH_LONG
                            ).show()
                        }
                    }
                    
                    override fun onTestFailed(testName: String, error: String) {
                        runOnUiThread {
                            isTestRunning = false
                            supportActionBar?.subtitle = null
                            
                            Toast.makeText(
                                this@CanvasTestActivity,
                                "$testName 失败: $error",
                                Toast.LENGTH_LONG
                            ).show()
                        }
                    }
                }
            )
        }
    }
    
    /**
     * 运行内存泄漏测试
     */
    private fun runMemoryTest() {
        if (isTestRunning) {
            Toast.makeText(this, "测试正在进行中，请稍候", Toast.LENGTH_SHORT).show()
            return
        }
        
        lifecycleScope.launch {
            isTestRunning = true
            
            canvasTester.runMemoryLeakTest(
                canvasView,
                object : CanvasTester.TestCallback {
                    override fun onTestStarted(testName: String) {
                        runOnUiThread {
                            Toast.makeText(this@CanvasTestActivity, "开始$testName", Toast.LENGTH_SHORT).show()
                        }
                    }
                    
                    override fun onTestProgress(progress: Int, message: String) {
                        runOnUiThread {
                            supportActionBar?.subtitle = "$message ($progress%)"
                        }
                    }
                    
                    override fun onTestCompleted(testName: String, result: CanvasTester.TestResult) {
                        runOnUiThread {
                            isTestRunning = false
                            supportActionBar?.subtitle = null
                            
                            val status = if (result.passed) "通过" else "失败"
                            val details = result.details.joinToString("\n")
                            
                            Toast.makeText(
                                this@CanvasTestActivity,
                                "$testName $status\n$details",
                                Toast.LENGTH_LONG
                            ).show()
                        }
                    }
                    
                    override fun onTestFailed(testName: String, error: String) {
                        runOnUiThread {
                            isTestRunning = false
                            supportActionBar?.subtitle = null
                            
                            Toast.makeText(
                                this@CanvasTestActivity,
                                "$testName 失败: $error",
                                Toast.LENGTH_LONG
                            ).show()
                        }
                    }
                }
            )
        }
    }
    
    /**
     * 停止当前测试
     */
    private fun stopCurrentTest() {
        if (isTestRunning) {
            canvasTester.stopTest()
            isTestRunning = false
            supportActionBar?.subtitle = null
            Toast.makeText(this, "测试已停止", Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(this, "没有正在运行的测试", Toast.LENGTH_SHORT).show()
        }
    }
    
    override fun onDestroy() {
        super.onDestroy()
        canvasTester.stopTest()
    }
}